package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.impl.SaturnSQL;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDataManager;

public class QueryBySaturnSQL {
	/**
	 * 
	 * @param fullKey
	 * @param sqlKey
	 * @param paramMap sqlKey所指向的sql中需要的参数
	 * @param conn
	 * @return
	 * @throws Exception
	 */
	public static List<SaturnData> execute(String fullKey, String sqlKey, Map<String, Object> paramMap,
			Connection conn) throws Exception {
		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(fullKey);
		String dbType = dataInfo.getDBType();
		String sql = null;
		sql = dataInfo.getSqlById(sqlKey, paramMap);
		
		SaturnSQL saturnSQL = new SaturnSQL();
		saturnSQL.setDBtype(dbType);
		saturnSQL.setOperationType(getBuilderType(fullKey));
		saturnSQL.setSaturnDataFullKey((String) fullKey);
		saturnSQL.setSql(sql);
		List<SaturnData> list = SaturnSQLHandler.executeQuery(saturnSQL, conn);
		return list;
	}
	private static String getBuilderType(String fullKey) {
		// 如果有大数据
		if (SaturnDataUtil.hasLOB(fullKey)) {
			return ISaturnSQL.SELECT_LOB_OPERATION;
		} else {
			return ISaturnSQL.SELECT_OPERATION;
		}
	}

//	private static boolean hasLOB(String fullKey) {
//		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(
//				fullKey);
//		for (String key : dataInfo.keySet()) {
//			IDataItem dataItem = dataInfo.getDataItem(key);
//			if (OracleAttribute.isLOB(dataItem.getType())) {
//				return true;
//			}
//		}
//		return false;
//	}
}
